package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.game.Cell;
import com.handinfo.android.game.IIcon;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 单元格控件
 * @author handinfo
 */
public class DWGrid extends DWControl
{
	public Object m_data = null;
	private Bitmap img_gird = null;
	private boolean m_select = false;
	private boolean m_noDrag = false;//是不是不能拖拽
	public static final byte SELECT_TYPE_RECT = 0;
	public static final byte SELECT_TYPE_ARC1 = 1;
	public static final byte SELECT_TYPE_ARC2 = 2;
	public static final byte SELECT_TYPE_ARC3 = 3;
	private byte m_select_type = SELECT_TYPE_RECT;
	/**
	 * 创建一个格子
	 * @param img
	 */
	public DWGrid(Bitmap img)
	{
		this(img, 0, 0);
	}

	/**
	 * 创建一个格子
	 * @param img
	 * @param x
	 * @param y
	 */
	public DWGrid(Bitmap img, int x, int y)
	{
		if (img == null) {
			Tools.debugPrintln("Grid Image is null!");
		}
		img_gird = img;
		setPoint(x, y);
		setShowWideHigh(img_gird.getWidth(), img_gird.getHeight());
	}
	
	/**
	 * 创建一个不需要背景的格子
	 * @param w
	 * @param h
	 */
	public DWGrid(int x, int y, int w, int h) {
		setPoint(x, y);
		setShowWideHigh(w, h);
	}

	/**
	 * 添加一个对象
	 * @param obj
	 */
	public void addObject(Object obj)
	{
		m_data = obj;
	}

	/**
	 * 删除当前格子中的对象
	 */
	public void removeObject()
	{
		m_data = null;
	}

	/**
	 * 设置格子图片
	 * @param img
	 */
	public void setImage(Bitmap img)
	{
		if (img == null) {
			Tools.debugPrintln("Grid setImage is null!");
		}
		img_gird = img;
	}
	/**
	 * 设置是否可以拖拽
	 * @param drag
	 */
	public void setDarg(boolean drag) {
		m_noDrag = drag;
	}
	/**
	 * 设置选择
	 * @param select
	 */
	public void setSelect(boolean select) {
		m_select = select;
	}
	/**
	 * 得到是否被选择
	 * @return
	 */
	public boolean getSelect() {
		return m_select;
	}
	
	/**
	 * 设置选择类型
	 * @param type
	 */
	public void setSelectType(byte type) {
		m_select_type = type;
	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x, m_show_y, m_show_w, m_show_h);
		int tempx = m_show_x + m_show_w / 2;
		int tempy = m_show_y + m_show_h / 2;
		if (img_gird != null) {
			g.drawBitmap(img_gird, tempx, tempy, Tools.HCENTER_VCENTER);
		}
		if (m_data != null) {
			if (m_data instanceof IIcon) {
				((IIcon)m_data).drawIcon(g, tempx, tempy, m_show_w, m_show_h, Tools.HCENTER_VCENTER);
			}
		}
		if (m_select) {
			if (m_select_type == SELECT_TYPE_RECT) {
				DWControlsManager.getInstance().paintSelectRect(g, m_show_x, m_show_y, m_show_w, m_show_h);
			} else if (m_select_type == SELECT_TYPE_ARC1) {
				DWControlsManager.getInstance().paintSelectArc1(g, m_show_x, m_show_y, m_show_w, m_show_h);
			} else if (m_select_type == SELECT_TYPE_ARC2) {
				DWControlsManager.getInstance().paintSelectArc2(g, m_show_x, m_show_y, m_show_w, m_show_h);
			} else if (m_select_type == SELECT_TYPE_ARC3) {
				DWControlsManager.getInstance().paintSelectArc3(g, m_show_x, m_show_y, m_show_w, m_show_h);
			}
		}
		renderRect(g);
		renderGuide(g);
	}

	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		int tempx = x + m_show_x + m_show_w / 2 - offsetx;
		int tempy = y + m_show_y + m_show_h / 2 - offsety;
		if (img_gird != null) {
			g.drawBitmap(img_gird, tempx, tempy, Tools.HCENTER_VCENTER);
		}
		if (m_data != null) {
			if (m_data instanceof IIcon) {
				((IIcon) m_data).drawIcon(g, tempx, tempy, m_show_w, m_show_h, Tools.HCENTER_VCENTER);
			}
		}
		if (m_select) {
			if (m_select_type == SELECT_TYPE_RECT) {
				DWControlsManager.getInstance().paintSelectRect(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
			} else if (m_select_type == SELECT_TYPE_ARC1) {
				DWControlsManager.getInstance().paintSelectArc1(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
			} else if (m_select_type == SELECT_TYPE_ARC2) {
				DWControlsManager.getInstance().paintSelectArc2(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
			} else if (m_select_type == SELECT_TYPE_ARC3) {
				DWControlsManager.getInstance().paintSelectArc3(g, x + m_show_x - offsetx, y + m_show_y - offsety, m_show_w, m_show_h);
			}
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
		renderGuide(g, x - offsetx, y - offsety);
	}

	public void logic()
	{
	}

	public void refreshControl()
	{
		
	}
	
	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			if (m_data != null) {
				return true;
			}
		}
		return false;
	}
	
	public boolean doLongPress(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			if (m_data != null && !m_noDrag) {
				if (m_data instanceof Cell) {
					if (((Cell)m_data).m_item != null) {
						DWControlsManager.getInstance().setDragControl(this);
						if (m_long_listener != null) {
							m_long_listener.OnLong(true);
						}
						return true;
					}
				} else {
					DWControlsManager.getInstance().setDragControl(this);
					if (m_long_listener != null) {
						m_long_listener.OnLong(true);
					}
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			DWControl dragControl = DWControlsManager.getInstance().getDragControl();
			if (m_drag_listener != null && dragControl != null) {
				m_drag_listener.OnDrag(DWControlsManager.getInstance().getDragControl());
			}
			return true;
		}
		return false;
	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			if (m_listener != null) {
				m_listener.OnClick();
			}
			setGuide(false);
			return true;
		}
		return false;
	}

}
